import { _decorator, Component, ITriggerEvent, Node } from 'cc';
import { Barrier } from './Barrier';
import { PrefsManager } from './PrefsManager';
import { Group, LV, WEAPONPROPERTY, WEAPONTYPE } from './GameConstant';
import { GameManager } from './GameManager';
import { GameUtil } from './GameUtil';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import Banner from '../Banner';
const { ccclass, property } = _decorator;

@ccclass('ShowStand')
export class ShowStand extends Barrier {

    @property(Node)
    Weapons: Node[] = [];

    IsCollider: boolean = false;

    protected onLoad(): void {
        super.onLoad();
        this.Collider.on(`onTriggerStay`, this.onTriggerStay, this);
    }

    protected start(): void {
        this.showWeapon();
    }

    showWeapon() {
        const index = PrefsManager.Instance.userData.haveWeapon[PrefsManager.Instance.userData.haveWeapon.length - 1];
        if (index >= this.Weapons.length) {
            this.Weapons[this.Weapons.length - 1].active = true;
            return;
        }
        for (let i = 0; i < this.Weapons.length; i++) {
            if (index == i) this.Weapons[i].active = true;
            else this.Weapons[i].active = false;
        }
    }

    // getNewWeapon() {
    //     const newWeapon = GameUtil.getAdjacentEnumCirculation(WEAPONTYPE, PrefsManager.Instance.userData.CurWeapon);
    //     PrefsManager.Instance.userData.CurWeapon = newWeapon;
    //     PrefsManager.Instance.userData.haveWeapon.push(newWeapon);
    // }

    onTriggerStay(event: ITriggerEvent) {
        if (this.IsCollider) return;
        if (event.otherCollider.getGroup() == Group.Weapon) {
            this.IsCollider = true;
            Banner.Instance.ShowBannerAd();
            Banner.Instance.ShowCustomAd();
            AudioManager.PlaySound(Audios.OpenTreasureBox);
            GameManager.Instance.gamePause();
            PrefsManager.Instance.userData.CurLV = GameUtil.getAdjacentEnumCirculation(LV, PrefsManager.Instance.userData.CurLV, true);
            PrefsManager.Instance.saveData();
            //获得新武器 -- 展示新武器界面
            GameManager.Instance.showNewWeaponPanel();
        } else if (event.otherCollider.getGroup() == Group.Bullet) {
            PoolManager.PutNode(event.otherCollider.node);

        }
    }

}


